package ygg.exporter.swf.tag
{
	import j.sus2maj7.encoding.*;
	import j.sus2maj7.printer.*;
	import j.sus2maj7.context.*;
	
	import flash.filesystem.*;
	import flash.utils.*;
	
	import ygg.exporter.swf.*;
	
	import ygg.printer.*;
	
	import ygg.encoding.swf.tag.*;
	import ygg.encoding.swf.tag.object.*;
	import ygg.encoding.swf.structure.*;
	import ygg.encoding.swf.abc.*;
	import ygg.encoding.swf.abc.object.*;
	
	public class DoABC2Exporter extends TagExporter
	{
		public function DoABC2Exporter()
		{
			super();
		}
		
		override public function export(o:*, context:IContext = null):void
		{
			trace('DoABC2Exporter');
			var abc:DoABC = o as DoABC;
			var abcCtx:AbcContext = abc.context;
			
			var className:String = '';
			var packageName:String = '';
			var content:String;
			var precontent:String;
			
			var ipf:IPrinterFactory = new AbcPrinterFactory()
			var f:File;
			
			var pc:PrinterContext = new PrinterContext();
			pc.tab = '    ';
			pc.indent = '';
			pc.vars.abcContext = abcCtx;
			var ip:IPrinter
			for each (var script:Script in o.context.scripts)
			{
				/*
				trace(script.name, script.members.length, script.slots.length );
				pc.indent = '';
				pc.vars.attr = '';
					
				ip = ipf.getPrinter(script.init);
				content = ip.printToString(script.init, pc ) ;
				f = File(context.vars.classDir).resolvePath('_' + script.qName + '.as');
				trace(f.nativePath);
				writeStringToFile(content, f);
				//*/
				precontent = '';
				content = '';
				var mi:Trait
				for each (mi in script.members) //should be 1 script per as file, last one is the extenrelaly visible
				{
					pc.indent = '';
					pc.vars.attr = '';
					precontent += content;
					ip = ipf.getPrinter(mi);
					content = ip.printToString(mi, pc);
					
				}
				
				content += precontent;
				f = File(context.vars.classDir).resolvePath(  getPath(mi) );
				//trace(f.nativePath);
				writeStringToFile(content, f);
				
				
			}
			
		}
		
		private function getPath(mi:Trait):String
		{
			trace(mi.packageName, mi.name);
			var j:int;
			var s:String = mi.packageName || '';
			
			while ((j = s.indexOf('.')) > -1)
			{
				s = s.replace('.', '/');
			}
			if (s.length) s += '/';
			
			s += mi.name + '.as';
			
			return s;
		}
	}
}